package com.atguigu.product.products.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.atguigu.common.valid.AddVaid;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.product.products.entity.PmsBrandEntity;
import com.atguigu.product.products.service.PmsBrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author qhw
 * @email qhw@gmail.com
 * @date 2021-08-03 19:57:10
 */
@RestController
@RequestMapping("product/brand")
public class PmsBrandController {
    @Autowired
    private PmsBrandService pmsBrandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("products:pmsbrand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pmsBrandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    @RequiresPermissions("products:pmsbrand:info")
    public R info(@PathVariable("brandId") Long brandId){
		PmsBrandEntity pmsBrand = pmsBrandService.getById(brandId);

        return R.ok().put("brand", pmsBrand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //, BindingResult result
   // @RequiresPermissions("products:pmsbrand:save")
    public R save(@Validated({AddVaid.class}) @RequestBody PmsBrandEntity pmsBrand){
//        if(result.hasErrors()){
//            Map<String,String> map=new HashMap<>();
//            //有问题的话返回指定的数据
//            result.getFieldErrors().forEach((m)->{
//                //获取错误提示
//                String defaultMessage = m.getDefaultMessage();
//                //获取错误属性名称
//                String field = m.getField();
//                map.put(field,defaultMessage);
//
//            });
//            return R.error(400,"数据检验有问题").put("data",map);
//
//        }else {
            pmsBrandService.save(pmsBrand);
            return R.ok();
        }





    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("products:pmsbrand:update")
    public R update(@RequestBody PmsBrandEntity pmsBrand){
		pmsBrandService.updateDeatil(pmsBrand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("products:pmsbrand:delete")
    public R delete(@RequestBody Long[] brandIds){
		pmsBrandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
